package de.tud.etit;

import java.io.File;
import java.io.IOException;

import org.apache.log4j.BasicConfigurator;
import org.eclipse.emf.ecore.EObject;
import org.moflon.moca.blip.parser.BlipParserAdapter;
import org.moflon.moca.blip.unparser.BlipUnparserAdapter;
import org.moflon.moca.blip.unparser.android.JavaUnparserAdapter;
import org.moflon.moca.blip.unparser.android.PropertiesUnparserAdapter;
import org.moflon.moca.blip.unparser.android.XmlUnparserAdapter;
import org.moflon.tie.TGGMain;
import org.moflon.util.eMoflonEMFUtil;

import BlipLanguage.AndroidTransformer;
import BlipLanguage.BlipApplication;
import BlipLanguage.BlipLanguageFactory;
import BlipLanguage.IosTransformer;
import BlipLanguage.util.BlipLanguageAdapterFactory;
import Moca.CodeAdapter;
import Moca.MocaFactory;
import MocaTree.Folder;
import MocaTree.MocaTreePackage;

public class BlipWorkflow {

	private static CodeAdapter codeAdapter;

	public static void main(String[] args) throws IOException {
		BasicConfigurator.configure();

		// Register parsers and unparsers
		codeAdapter = MocaFactory.eINSTANCE.createCodeAdapter();
		codeAdapter.getParser().add(new BlipParserAdapter());
		codeAdapter.getUnparser().add(new BlipUnparserAdapter());

		// Perform text-to-tree
		Folder tree = codeAdapter.parse(new File("instances/in/"));

		// Save tree to file
		eMoflonEMFUtil.saveModel(tree, "instances/tree.xmi");

		// Perform tree-to-model

		// Set up logging
		BasicConfigurator.configure();

		// Forward Transformation
		TGGMain helper = new TGGMain();
		helper.performForward("instances/tree.xmi");
		EObject application = helper.getTrg();

		;

		transformForAndroid((BlipApplication) application);

		transformForIos((BlipApplication) application);
	}

	private static void transformForIos(BlipApplication application) {
		System.out.println("### Codegenerierung für iOS ###");
		// clean output directory
		DirectoryUtils.cleanupDirectory("./instances/out/ios");
		
		System.out.println("Beginn der Codegenerierung");
		
		IosTransformer iosTransformer = BlipLanguageFactory.eINSTANCE
				.createIosTransformer();
		Folder iosFolder = iosTransformer
				.transformApplication((BlipApplication) application);
		eMoflonEMFUtil.saveModel(iosFolder, "instances/iosTree.xmi");

		// Perform unparse for iOS
		codeAdapter.unparse("instances/out", iosFolder);
		
		System.out.println("Codegenerierung abgeschlossen. Anreicherung mit statischen Dateien.");
		
		// Enhance with static code
		ZipDecompressor.unzip("./static/ios.zip", "./instances/out/ios");
	}

	private static void transformForAndroid(BlipApplication application) {
		System.out.println("### Codegenerierung für Android ###");
		// clean output directory
		DirectoryUtils.cleanupDirectory("./instances/out/android");
		
		System.out.println("Beginn der Codegenerierung");
		CodeAdapter androidCodeAdapter = MocaFactory.eINSTANCE
				.createCodeAdapter();
		androidCodeAdapter.getUnparser().add(new PropertiesUnparserAdapter());
		androidCodeAdapter.getUnparser().add(new XmlUnparserAdapter());
		androidCodeAdapter.getUnparser().add(new JavaUnparserAdapter());
		AndroidTransformer androidTransformer = BlipLanguageFactory.eINSTANCE
				.createAndroidTransformer();
		Folder androidFolder = androidTransformer
				.transformApplication(application);
		eMoflonEMFUtil.saveModel(androidFolder, "instances/androidTree.xmi");
		
		// Perform unparse for android
		androidCodeAdapter.unparse("instances/out", androidFolder);
		
		System.out.println("Codegenerierung abgeschlossen. Anreicherung mit statischen Dateien.");
		
		// Enhance with static code
		ZipDecompressor.unzip("./static/android.zip", "./instances/out/android");
	}
}